#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/08/01
# @Author   :Changshu
# @File     :practice_13.py
# 13. 罗马数字转整数

''' 使用传统的判断
def romanToInt(s:str)->int:
	total=0
	if s=='':
		return total
	while 'IV' in s:
		total+=4
		s=s.replace('IV','',1)
	while 'IX' in s:
		total+=9
		s = s.replace('IX', '', 1)
	while 'XL' in s:
		total+=40
		s = s.replace('XL', '', 1)
	while 'XC' in s:
		total+=90
		s = s.replace('XC', '', 1)
	while 'CD' in s:
		total+=400
		s = s.replace('CD', '', 1)
	while 'CM' in s:
		total+=900
		s = s.replace('CM', '', 1)
	for x in s:
		if x=='I':
			total+=1
		if x=='V':
			total+=5
		if x=='X':
			total+=10
		if x=='L':
			total+=50
		if x=='C':
			total+=100
		if x=='D':
			total+=500
		if x=='M':
			total+=1000
	return total
'''

# 使用字典
def romanToInt(s:str)->int:
	dict={'IV':0,'IX':0,'XL':0,'XC':0,'CD':0,'CM':0,'I':0,'V':0,'X':0,'L':0,'C':0,'D':0,'M':0}
	total=0
	if 'IV' in s:
		dict['IV']=s.count('IV')
		s=s.replace('IV','')
	if 'IX' in s:
		dict['IX']=s.count('IX')
		s=s.replace('IX','')
	if 'XL' in s:
		dict['XL']=s.count('XL')
		s=s.replace('XL','')
	if 'XC' in s:
		dict['XC']=s.count('XC')
		s=s.replace('XC','')
	if 'CD' in s:
		dict['CD']=s.count('CD')
		s=s.replace('CD','')
	if 'CM' in s:
		dict['CM']=s.count('CM')
		s=s.replace('CM','')
	if 'I' in s:
		dict['I']=s.count('I')
		s=s.replace('I','')
	if 'V' in s:
		dict['V']=s.count('V')
		s=s.replace('V','')
	if 'X' in s:
		dict['X']=s.count('X')
		s=s.replace('X','')
	if 'L' in s:
		dict['L']=s.count('L')
		s=s.replace('L','')
	if 'C' in s:
		dict['C']=s.count('C')
		s=s.replace('C','')
	if 'D' in s:
		dict['D']=s.count('D')
		s=s.replace('D','')
	if 'M' in s:
		dict['M']=s.count('M')
		s=s.replace('M','')
	total+=dict['IV']*4+dict['IX']*9+dict['XL']*40+dict['XC']*90+dict['CD']*400+dict['CM']*900+dict['I']*1+dict['V']*5+\
		   dict['X']*10+dict['L']*50+dict['C']*100+dict['D']*500+dict['M']*1000
	return total


if __name__=='__main__':
	s='MCMXCIV'
	# print(s.count('M'))
	print(romanToInt(s))